define([
    "UtilDir/util",
    "UtilDir/tool",
    "Echarts",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, Echarts) {

    var sysPath = getServer() + "/static/app/fwsjcj";

    var zwFormHtml = sysPath + "/brick/views/zwForm.html";

    /**
     * 页面初始化
     */
    var init = function () {
        initProvince()
        queryBtnBind();
        clickTabs();
        createCharts('', '', '', 0)
    };
    var currentTab = 1
    var initProvince = function () {
        var html = "";
        $.ajax({
            url: "/app/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#belongtoProvince").append(html);
            }
        });
    }
    var queryBtnBind = function () {
        $("#btnSearch").click(function () {
            var belongtoProvince = $('#belongtoProvince').val()
            var taskName = $('#taskName').val()
            var status = $('#status').val()
            if (currentTab == 1) {
                createCharts(belongtoProvince, taskName, status, 0)
            } else if (currentTab == 2) {
                createCharts(belongtoProvince, taskName, status, 1)
            }
        });
        $("#btnReset").click(function () {
            $('#belongtoProvince').val('')
            $('#taskName').val('')
            $('#status').val('')
        });
    };

    var clickTabs = function () {
        $('#myTab').on('shown.bs.tab', function (e) {
            $('#belongtoProvince').val('')
            $('#taskName').val('')
            $('#status').val('')
            var tabid = $(e.target).attr('id');
            if (tabid == 'number') {
                currentTab = 1
                createCharts('', '', '', 0);
            } else if (tabid == 'area') {
                currentTab = 2
                createCharts('', '', '', 1);
            }
        })
    }
    var createCharts = function (belongtoProvince, taskName , status , type) {
        var params = {
            belongtoProvince,
            taskName,
            status
        }
        if (type == 0) {
            $('#contentCharts').html(`
                <div id='NumberBar' style='width:50%;height:510px; float: left;'></div>
                <div id='NumberPie' style='width:50%;height:510px; float: left;'></div>
            `)
            var NumberBar = Echarts.init(document.getElementById('NumberBar'));
            var NumberPie = Echarts.init(document.getElementById('NumberPie'));
            $.ajax({
                url: getServer() + "/fw/structure/buildingstype",
                contentType: "application/json",
                method: "GET",
                data: params,
                success: function (data) {
                    let pieData = []
                    for (let i = 0; i < data.buildCountMap.x.length; i++) {
                        pieData.push({
                            value: data.buildCountMap.y[i],
                            name: data.buildCountMap.x[i]
                        })
                    }
                    let options = {
                        NumberBar: {
                            title: {
                                text: '栋数-柱状',
                                left: 'center'
                            },
                            xAxis: {
                                type: 'category',
                                data: data.buildCountMap.x,
                                axisLabel: {
                                    rotate: 45, //倾斜度 -90 至 90 默认为0           
                                },
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    margin: 4
                                }
                            },
                            series: [{
                                data: data.buildCountMap.y,
                                type: 'bar',
                                showBackground: true,
                                backgroundStyle: {
                                    color: 'rgba(220, 220, 220, 0.8)'
                                }
                            }],
                            tooltip: {
                                trigger: 'item'
                            },
                        },
                        NumberPie: {
                            title: {
                                text: '栋数-饼状',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'item',
                                formatter: '{a} <br/>{b} : {c} ({d}%)'
                            },
                            legend: {
                                data: data.buildCountMap.x,
                                top: 'bottom',
                                left: 'center',
                            },
                            series: [{
                                name: '栋数统计',
                                type: 'pie',
                                radius: '50%',
                                data: pieData,
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }]
                        }
                    }
                    NumberBar.setOption(options.NumberBar);
                    NumberPie.setOption(options.NumberPie);

                }
            });
        } else {
            $('#contentCharts').html(
                `
                <div id='AreaBar' style='width:50%;height:510px; float: left;'></div>
                <div id='AreaPie' style='width:50%;height:510px; float: left;'></div>
                `
            )
            var AreaBar = Echarts.init(document.getElementById('AreaBar'));
            var AreaPie = Echarts.init(document.getElementById('AreaPie'));
            $.ajax({
                url: getServer() + "/fw/structure/areacount",
                contentType: "application/json",
                method: "GET",
                data: params,
                success: function (data) {
                    let pieData = []
                    for (let i = 0; i < data.areaCountMap.x.length; i++) {
                        pieData.push({
                            value: data.areaCountMap.y[i],
                            name: data.areaCountMap.x[i]
                        })
                    }
                    let options = {
                        AreaBar: {
                            title: {
                                text: '面积-柱状',
                                left: 'center'
                            },
                            xAxis: {
                                type: 'category',
                                data: data.areaCountMap.x,
                                axisLabel: {
                                    rotate: 45, //倾斜度 -90 至 90 默认为0           
                                },
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    margin: 4
                                }
                            },
                            series: [{
                                data: data.areaCountMap.y,
                                type: 'bar',
                                showBackground: true,
                                backgroundStyle: {
                                    color: 'rgba(220, 220, 220, 0.8)'
                                }
                            }],
                            tooltip: {
                                trigger: 'item',
                            },
                        },
                        AreaPie: {
                            title: {
                                text: '面积-饼状',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'item',
                                formatter: '{a} <br/>{b} : {c} ({d}%)'
                            },
                            legend: {
                                data: data.areaCountMap.x,
                                top: 'bottom',
                                left: 'center',
                            },
                            series: [{
                                name: '面积统计',
                                type: 'pie',
                                radius: '50%',
                                data: pieData,
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }]
                        }
                    }
                    AreaBar.setOption(options.AreaBar);
                    AreaPie.setOption(options.AreaPie);
                }
            });
        }
    }
    return {
        init: init
    };
});